<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Upload
 * @copyright  Copyright (c) 2005-2007 Axel ETCHEVERRY (http://axel.2ate.net)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @author Axel ETCHEVERRY
 * @link http://axel.2ate.net
 * @email euskadi31[at]gmail[dot]com
 * @version 1.0
 */

require_once 'Zend/Upload/Exception.php';

class Zend_Upload 
{

	const X = 1000;

	/*
	* @var string
	*/
	protected $file;
	
	/*
	* @var string
	*/
	protected $path;
	
	/*
	* @var string
	*/
	protected $uri;
	
	/*
	* @var string
	*/
	protected $extentionFilter = null;
	
	/*
	* @var string
	*/
	protected $name;
	
	/*
	* @var string
	*/
	protected $fileExtention;
	
	/*
	* @var int
	*/
	protected $maxSize;

	/*
	* @var int
	*/
	protected $minSize = 0;

	/*
	* @access public
	* @param  string    $file
	* @param  string    $path
	*/
	public function __construct($file,$path) {
		$this->file = $_FILES[$file];
		$this->path = $path;
		$ext = explode(".", $_FILES[$file]['name']);
		$this->fileExtention = array_pop($ext);
	}
	
	/*
	* @access public
	* @param  string    $ext
	* @return Zend_Upload Provides fluent interface
	*/
	public function setExtentionFilter($ext) {
		//$ext = "png|jpg|jpeg|gif";
		$this->extentionFilter = $ext;
		return $this;
	}
	
	/*
	* @access public
	* @param  string    $path
	* @return Zend_Upload Provides fluent interface
	*/
	public function setPath($path) {
		$this->path = $path;
		return $this;
	}
	
	/*
	* @access public
	* @param  string    $name
	* @return Zend_Upload Provides fluent interface
	*/
	public function setName($name) {
		$this->name = $name;
		return $this;
	}

	/*
	* @access public
	* @param  string    $name
	* @return Zend_Upload Provides fluent interface
	*/
	public function setMaxSize($size) {
		$this->maxSize = $size * Zend_Upload::X;
		return $this;
	}

	/*
	* @access public
	* @param  string    $name
	* @return Zend_Upload Provides fluent interface
	*/
	public function setMinSize($size) {
		$this->minSize = $size * Zend_Upload::X;
		return $this;
	}

	/*
	* @access public
	* @return Zend_Upload Provides fluent interface
	*/
	
	/*
		UP101 = File size more than max size
		UP102 = File size small than min size
		UP103 = Extention error 
		UP104 = Upload file failed
		UP105 = Don't have file
	*/
	
	public static function isFileUpload($file) 
	{
		return !empty($_FILES[$file]['name']);
	}
	
	public function upload() 
	{
		if(!empty($this->file['name'])) 
		{
			if($this->file['size'] >= $this->minSize)
			{
				if($this->maxSize != null && $this->file['size'] >= $this->maxSize)
				{
					throw new Zend_Upload_Exception("UP101");
					return $this;
				}
			}
			else 
			{
				throw new Zend_Upload_Exception("UP102");
				return $this;
			}
			
			$this->_extention($this->file['name']);
			if(!empty($this->name)) 
			{
				$this->file['name'] = $this->name.".".$this->fileExtention;
			}
			
			$this->uri = $this->path . "/" . basename($this->file['name']);

			if(eregi("(".$this->extentionFilter.")", $this->file['name'])) 
			{
				if(!@move_uploaded_file($this->file['tmp_name'], $this->uri)) 
				{
					throw new Zend_Upload_Exception("UP104");
					return $this;
				}
				@chmod($this->uri, 0644);
				return $this;
			}else
			{
				throw new Zend_Upload_Exception("UP103");
				return $this;
			}
		}
		else
		{
			throw new Zend_Upload_Exception("UP105");
			return $this;
		}
	}

	public function upload_and_fix_img_size($imgName,$widthSize,$heightSize,$deleteImgRoot = false) 
	{
		$this->extentionFilter = "png|jpg|jpeg|gif";
		if($this->uri == null)
			$this->upload();
		//$newFileName = $imgName . '.' . $this->fileExtention;	
		$newPathName = $this->path . "/" . $imgName;			
		$this->_upload_and_fix_img_size($this->uri,$newPathName,$widthSize,$heightSize);
		if($deleteImgRoot) 
		{
			unlink($this->uri);
			$this->uri = null;
		}
		return $newFileName;
		
	}
	
	private function _upload_and_fix_img_size($dirUpload,$dirFix,$widthSize,$heightSize)
	{
		$original = $dirUpload;
		$fix = $dirFix;
		
		list($width, $height,$type) = getimagesize($original) ; 
		
		if($width <= $widthSize && $height <= $heightSize)
		{
			$ChoiceWith 	= $width;
			$ChoiceHeight 	= $height;
		}else{
			if($width > $height && $width > $widthSize)
			{
				$radioWidth		= $widthSize/$width;
				$ChoiceWith 	= $radioWidth * $width;
				$ChoiceHeight 	= $radioWidth * $height;
			}
			
			if($width > $height && $width < $widthSize)
			{
				$ChoiceWith 	= $width;
				$ChoiceHeight 	= $height;
			}
			
			if($height > $width && $height > $heightSize)
			{
				$radioHeight	= $heightSize/$height;
				$ChoiceWith 	= $radioHeight * $width;
				$ChoiceHeight 	= $radioHeight * $height;
			}
			if($height > $width && $height < $heightSize)
			{
				$ChoiceWith 	= $width;
				$ChoiceHeight 	= $height;
			}
			
		}
		
		if($type == 2)
		{			
			@header("Content-type: image/jpeg");    	
			$tn = imagecreatetruecolor($ChoiceWith, $ChoiceHeight) ; 
			$image = imagecreatefromjpeg($original) ; 
			imagecopyresampled($tn, $image, 0, 0, 0, 0, $ChoiceWith, $ChoiceHeight, $width, $height) ; 	
			imagejpeg($tn, $fix, 80) ; 
		}
		
		if($type == 1)
		{
			@header("Content-type: image/gif");    	
			$tn = imagecreatetruecolor($ChoiceWith, $ChoiceHeight) ; 
			$image = imagecreatefromgif($original) ; 
			imagecopyresampled($tn, $image, 0, 0, 0, 0, $ChoiceWith, $ChoiceHeight, $width, $height) ; 	
			imagegif($tn, $fix) ; 
		}
		
	}
	
	
	/*
	* @access protected
	* @param  string    $filename
	*/
	protected function _extention($filename) {
		$ext = explode(".", $filename);
		$this->fileExtention = array_pop($ext);
	}
	
	/*
	* @access public
	* @return string
	*/
	public function getName() {
		return $this->file['name'];
	}
	
	/*
	* @access public
	* @return string
	*/
	public function getUri() {
		return $this->uri;
	}
	
	/*
	* @access public
	* @return string
	*/
	public function getPath() {
		return $this->path;
	}
	
	/*
	* @access public
	* @return string
	*/
	public function getFileExtention() {
		return $this->fileExtention;
	}

}

?>